package com.tdam2012.grupo04;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;

import com.tdam2012.grupo04.data.ConectividadProvider;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;


public class ConectividadReceiver extends BroadcastReceiver {
	private static final int NOTIF_ALERTA_ID = 0;
	private static final String GTM="GMT-03:00";
	private static final String FORMATO="dd/MM/yyyy HH:mm:ss";
	Integer conexion;
	String tipo;
	String fecha;
	
	@Override
	public void onReceive(Context context, Intent intent) {
		
    	if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
    		NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
    		String typeName = info.getTypeName();
    		boolean available = info.isConnectedOrConnecting();
	  
	    
    		Calendar c = Calendar.getInstance(TimeZone.getTimeZone(GTM)); 
    		SimpleDateFormat postFormater = new SimpleDateFormat(FORMATO); 
    		Integer bandera;
    		String conectado;
    	
    		if (available) {
    			bandera=1;
    			conectado=context.getResources().getString(R.string.ConectividadListActivity_conexionHabilitada);
    		}
    		else{
    			bandera=0;
    			conectado=context.getResources().getString(R.string.ConectividadListActivity_conexionDeshabilitada);
    		}
    		if (typeName.equalsIgnoreCase("mobile")) {
				typeName=context.getResources().getString(R.string.ConectividadListActivity_conexionDatos);
			}else{
				typeName=context.getResources().getString(R.string.ConectividadListActivity_conexionDatos);
			}
	        	
    		//Grabo algunos registros
    		ContentValues cv = new ContentValues();				
    		cv.put(ConectividadProvider.COL_CONEXION, bandera);
    		cv.put(ConectividadProvider.COL_TIPO, typeName);
    		cv.put(ConectividadProvider.COL_FECHA, postFormater.format(c.getTime()));

    		context.getContentResolver().insert(ConectividadProvider.CONTENT_URI, cv);

	    NotifiacionSimple(context, conectado, typeName);
	    
	}
		
	}
	public void NotifiacionSimple(Context _context, String conectado, String type){
    	String ns = Context.NOTIFICATION_SERVICE; 
		NotificationManager notManager = (NotificationManager) _context.getSystemService(ns); 
		
		
		int icono = android.R.drawable.stat_sys_warning;
		CharSequence textoEstado = _context.getResources().getString(R.string.ConectividadReceiver_aviso);
		long hora = System.currentTimeMillis(); 
		 
		Notification notif = new Notification(icono, textoEstado, hora);
		
		
		Context contexto = _context.getApplicationContext();
		CharSequence titulo = conectado;
		CharSequence descripcion =type;
		 
		Intent notIntent = new Intent(contexto,ConectividadListActivity.class); 
		 
		PendingIntent contIntent = PendingIntent.getActivity(contexto, 0, notIntent, 0); 
		 
		notif.setLatestEventInfo(contexto, titulo, descripcion, contIntent); 
		
		notif.flags |= Notification.FLAG_AUTO_CANCEL; 
		//notif.defaults |= Notification.DEFAULT_SOUND;
		//notif.defaults |= Notification.DEFAULT_VIBRATE;
		//notif.defaults |= Notification.DEFAULT_LIGHTS;
		
		notManager.notify(NOTIF_ALERTA_ID,notif);	
		
		
		
		
	}
	

}
